home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15486 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.6 KB  |  94 lines

  1. Path: sm-igw1.geoplex.com!usenet
  2. From: "Jay B. Perry" <jay@geoplex.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: const member functions
  5. Date: Fri, 05 Apr 1996 12:56:06 -0800
  6. Organization: AT & T
  7. Message-ID: <316588E6.7D61@geoplex.com>
  8. NNTP-Posting-Host: 204.160.245.22
  9. Mime-Version: 1.0
  10. Content-Type: multipart/mixed; boundary="------------52476AAA66EB"
  11. X-Mailer: Mozilla 2.0 (Win95; I)
  12. CC: jay@geoplex.com
  13.  
  14. This is a multi-part message in MIME format.
  15.  
  16. --------------52476AAA66EB
  17. Content-Type: text/plain; charset=us-ascii
  18. Content-Transfer-Encoding: 7bit
  19.  
  20. The attached code doesn't compile, complaining about the non-const
  21. object's attempt to use a non-const private method (even though a
  22. const public method with the same name is available).
  23.  
  24. After leafing through a couple of references I have on C++, I have
  25. come to the conclusion that the attached C++ code should be
  26. compilable.
  27.  
  28. Could someone explain to me why this code doesn't work?  I have
  29. tried compiling it using VC++4.1, SunSoft CC SC3.0.1, g++ 2.7.x
  30. (I am not sure what the current version is, but I know that the
  31. most current version was used).
  32.  
  33. My theory is that it is bagging it because it can't differentiate
  34. between the int& and the int.  But, I also had hoped that in the
  35. context of the call, it should be able to realize that it doesn't
  36. have access to the private method, so let's see if there is a public
  37. method that can be used.
  38.  
  39. As far as options, I just attempted to produce an executable:
  40.  
  41. g++ -o file file.cc  (using g++ as an example)
  42.  
  43. -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  44. Jay B. Perry, AT&T            | jay@geoplex.com
  45. 2929 Campus Drive - Suite 350 | V: (415) 577-7747
  46. San Mateo, CA 94403           | F: (415) 577-7711
  47.  
  48. --------------52476AAA66EB
  49. Content-Type: text/plain; charset=us-ascii
  50. Content-Transfer-Encoding: 7bit
  51. Content-Disposition: inline; filename="bug.cpp"
  52.  
  53. //
  54. //  Code starts here
  55. //
  56. #include <stdlib.h>
  57. #include <iostream.h>
  58.  
  59. class X 
  60. {
  61. public:   //  Public methods
  62.    X( )           : _value( 0 ) { }
  63.    X( int value ) : _value( value ) { }
  64.  
  65.    //  Public accessor, should be available to all
  66.    //  X objects.
  67.    int value( ) const { return _value; }
  68.  
  69. private:  //  Private methods
  70.    //  Private accessor, should only be available to
  71.    //  non-const X objects
  72.    int& value( ) { return _value; }
  73.  
  74. private:  //  Private data members
  75.    int _value;
  76. };
  77.  
  78. main( )
  79. {
  80.    X t1( 1 );
  81.    const X t2( 2 );
  82.  
  83.    //  The following line results in an error for several
  84.    //  compilers, all complaining about an attempt to use
  85.    //  the private X::value method!
  86.    cout << t1.value() << endl;
  87.  
  88.    //  This line
  89.    cout << t2.value() << endl;
  90. }
  91.  
  92. --------------52476AAA66EB--
  93.  
  94.